﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Wallpaper.Models.Entities;

namespace Wallpaper.Data.Configurations
{
    public class RoleMenuConfiguration : IEntityTypeConfiguration<RoleMenu>
    {
        public void Configure(EntityTypeBuilder<RoleMenu> builder)
        {
            // 表名配置
            builder.ToTable("role_menus");

            // 复合主键配置（RoleId + MenuId）
            builder.HasKey(rm => new { rm.RoleId, rm.MenuId });

            // 字段配置
            builder.Property(rm => rm.RoleId)
                .IsRequired()
                .HasMaxLength(36)
                .HasComment("角色ID");

            builder.Property(rm => rm.MenuId)
                .IsRequired()
                .HasMaxLength(36)
                .HasComment("菜单ID");

            builder.Property(rm => rm.IsDeleted)
                .IsRequired()
                .HasDefaultValue(false)
                .HasComment("软删除标志");

            // 索引配置
            builder.HasIndex(rm => rm.RoleId)
                .HasDatabaseName("IX_RoleMenu_RoleId");

            builder.HasIndex(rm => rm.MenuId)
                .HasDatabaseName("IX_RoleMenu_MenuId");

            builder.HasIndex(rm => rm.IsDeleted)
                .HasDatabaseName("IX_RoleMenu_IsDeleted");

            // 外键关系配置
            builder.HasOne(rm => rm.Role)
                .WithMany(r => r.RoleMenus)
                .HasForeignKey(rm => rm.RoleId)
                .OnDelete(DeleteBehavior.Cascade);

            builder.HasOne(rm => rm.Menu)
                .WithMany(m => m.RoleMenus)
                .HasForeignKey(rm => rm.MenuId)
                .OnDelete(DeleteBehavior.Cascade);

            // 数据种子（可选 - 为超级管理员分配所有菜单权限）
            // builder.HasData(RoleMenuSeedData.GetSeedData());
        }
    }
}